package com.framework.core.tag;

import com.framework.core.constant.Globals;
import com.framework.core.util.StringUtil;
import com.framework.web.sys.domain.Resource;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.Map;

/**
 * 按钮自定义标签
 */
public class ButtonTag extends TagSupport {
    private static final long serialVersionUID = 5083787480718845899L;
    private String action;//唯一码
    private String className = "easyui-linkbutton";//按钮样式
    private String iconCls;//图标
    private String plain = "true";//是否显示背景
    private String onclick;//事件
    private String buttonName;//按钮名称

    /**
     *
     * 标签的开始
     */
    @SuppressWarnings({"unchecked", "static-access"})
    @Override
    public int doStartTag() throws JspException {
        //从session中获取当前用户的所有权限
        JspWriter out = this.pageContext.getOut();
        Map<String, Object> map = (Map<String, Object>) this.pageContext.getSession().getAttribute(Globals.USER_BUTTON_RES);
        Resource o = (Resource) map.get(action);
        if (o == null) {
            return super.SKIP_BODY;
        } else {
            try {
                String button = end(o).toString();
                out.print(button);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return super.EVAL_BODY_AGAIN;
        }
    }

    public StringBuffer end(Resource resource) {
        StringBuffer sb = new StringBuffer();
        sb.append("<a href=\"javascript:void(0)\" class=\"");
        sb.append(className);
        sb.append("\"");
        if (StringUtil.isEmpty(iconCls)) {
            iconCls = resource.getIconCls();
        }
        if (StringUtil.isNotEmpty(iconCls)) {
            sb.append(" iconCls=\"");
            sb.append(iconCls);
            sb.append("\"");
        }
        sb.append(" plain=\"");
        sb.append(plain);
        sb.append("\"");

        if (StringUtil.isEmpty(onclick)) {
            onclick = resource.getClickFun();
        }
        if (StringUtil.isNotEmpty(onclick)) {
            sb.append("   onclick=\"");
            sb.append(onclick);
            sb.append("\"");
        }
        sb.append(">");
        sb.append(buttonName);
        sb.append("</a>");
        return sb;
    }


    public String getOnclick() {
        return onclick;
    }

    public void setOnclick(String onclick) {
        this.onclick = onclick;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getIconCls() {
        return iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public String getPlain() {
        return plain;
    }

    public void setPlain(String plain) {
        this.plain = plain;
    }

    public String getButtonName() {
        return buttonName;
    }

    public void setButtonName(String buttonName) {
        this.buttonName = buttonName;
    }
}
